package com.baizhi.springboot.controller;

import com.baizhi.springboot.entity.*;
import com.baizhi.springboot.service.CarService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@RestController
public class CarController {
    @Autowired
    private CarService carService;

    @RequestMapping("/selectCarByPage")
    public Map selectCarByPage(Integer page, Integer limit) {
        return carService.selectCarByPage(page,limit);
    }

    @RequestMapping("/insertCard")//添加图书表单
    public Map insertCard(Card card) {

        HashMap<Object, Object> map = new HashMap<>();

        try {
            carService.insertCard(card);
            map.put("isAdd", true);
        } catch (Exception e) {
            e.printStackTrace();
            map.put("isAdd", false);
        }
        return map;
    }
    @RequestMapping("/insertBook")//添加图书表单
    public Map insertBook(Book book) {

        HashMap<Object, Object> map = new HashMap<>();

        try {
            carService.insertBook(book);
            map.put("isAdd", true);
        } catch (Exception e) {
            e.printStackTrace();
            map.put("isAdd", false);
        }
        return map;
    }
    @RequestMapping("/insertSeal")//添加印章表单
    public Map insertSeal(Seal seal) {

        HashMap<Object, Object> map = new HashMap<>();

        try {
            carService.insertSeal(seal);
            map.put("isAdd", true);
        } catch (Exception e) {
            e.printStackTrace();
            map.put("isAdd", false);
        }
        return map;
    }
    @RequestMapping("/insertCarApplyFor")//添加汽车申请表单
    public Map insertCarApplyFor(CarApplyFor carApplyFor) {

        HashMap<Object, Object> map = new HashMap<>();

        try {
            carService.insertCarApplyFor(carApplyFor);
            map.put("isAdd", true);
        } catch (Exception e) {
            e.printStackTrace();
            map.put("isAdd", false);
        }
        return map;
    }
    @RequestMapping("/insertCar")//添加表单
    public Map insertCar(Car car) {

        HashMap<Object, Object> map = new HashMap<>();

        try {
            carService.insertCar(car);
            map.put("isAdd", true);
        } catch (Exception e) {
            e.printStackTrace();
            map.put("isAdd", false);
        }
        return map;
    }
    @RequestMapping("/deleteCar")
    public Map  deleteCar(Integer id){
        HashMap<Object, Object> map = new HashMap<>();

        try {
            carService.deleteCar(id);
            map.put("isDelete", true);
        } catch (Exception e) {
            e.printStackTrace();
            map.put("isDelete", false);
        }
        return map;
    }

    @RequestMapping("/selectOneCar")
    public Map selectOneCar(Integer id, HttpSession session) {
        Car car= carService.selectOneCar(id);
        HashMap<Object, Object> map = new HashMap<>();
        map.put("car", car);
        return map;
    }

    @RequestMapping("/updateCar")
    public Map updateCar(Car car) {
        HashMap<Object, Object> map = new HashMap<>();
        try {
            //System.out.println(label);
            carService.updateCar(car);
            map.put("isUpdate", true);
        } catch (Exception e) {
            e.printStackTrace();
            map.put("isUpdate", false);
        }
        return map;
    }

}
